
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Diagnostics;
using System.Xml;
using Swaf.BizObj;

namespace Swaf.Call
{
	/// <summary>
	/// Summary description for ObjectCall.
	/// </summary>
	public class ObjectCall : BaseCall
	{
		protected DynamicCall m_call;

		public ObjectCall(string id, XmlElement initInfo)
			:base(id, initInfo)
		{
			XmlElement constructorParams = (XmlElement) initInfo.SelectSingleNode("ConstructorParams");
			if (constructorParams!= null)
				m_call = new DynamicCall(m_className, m_methodName, constructorParams.SelectNodes("Param"));
			else
			{
				try
				{
					m_call = new DynamicCall(m_className, m_methodName);
				}
				catch(Exception e)
				{
					throw new CallException(String.Format("Unable to create instance of class: {0}", m_className), e);
				}
			}
		}
		public ObjectCall(object onThis, string id, XmlElement initInfo)
			: base(id, initInfo)
		{
			Debug.Assert(onThis != null);
			m_className = onThis.GetType().FullName;
			try
			{
				m_call = new DynamicCall(onThis, m_methodName);
			}
			catch (Exception e)
			{
				throw new CallException(String.Format("Unable to create instance of class: {0}", m_className), e);
			}

		}

		public ObjectCall(IBizObj callDef) : this((string) callDef.get("Id"), (string) callDef.get("name"), (string) callDef.get("methodName"))
		{
			
		}
		public ObjectCall(object onThis, string method, params object[] p)
			:base("", method, p)
		{
			Debug.Assert(onThis != null);
			m_className = onThis.GetType().FullName;
			m_call = new DynamicCall(onThis, method);
		}

		public ObjectCall(string id, string className, string methodName, params object[] p)
			:base(id, methodName, p)
		{
			Debug.Assert(className != null);
			m_className = className;
			m_call = new DynamicCall(className, methodName);
		}

		public override object call(params object[] p)
		{
			Debug.Assert(m_call != null, "m_call should never be null");
			return m_call.call(this.calcCallParams(p));
		}

		public DynamicCall CallObject
		{
			get { return m_call; }
			set { m_call = value; }
		}

	}
}
